package org.tloss.web.editor.form.editors.command;

import org.eclipse.core.resources.IResourceChangeListener;
import org.tloss.web.editor.form.editors.BodyProvider;
import org.tloss.web.editor.model.command.Command;

public class BodyCommandElement extends AbstractCommandElement {
	BodyProvider form;

	public BodyCommandElement(BodyProvider form, IResourceChangeListener listener) {
		super(null, listener);
		setForm(form);
	}

	public AbstractCommandElement getInstanceDeeply(
			AbstractCommandElement parent, Command command,
			IResourceChangeListener listener) {
		AbstractCommandElement abstractCommandElement = ElementFactory
				.getInstance(parent, command, listener);
		for (int i = 0; command.getChildren() != null
				&& i < command.getChildren().size(); i++) {
			abstractCommandElement.add(getInstanceDeeply(
					abstractCommandElement, command.getChildren().get(i),
					listener));
		}
		return abstractCommandElement;
	}

	public void setForm(BodyProvider form) {
		this.form = form;
		if (form != null) {
			this.command = form.getBody();
			if (abstractCommandElements.isEmpty()
					&& form.getBody().getChildren() != null) {
				for (int i = 0; i < form.getBody().getChildren().size(); i++) {
					abstractCommandElements.add(getInstanceDeeply(this, form
							.getBody().getChildren().get(i), listener));
				}
			}
		}
	}

	public boolean isSupportedChildren() {
		return true;
	}

	@SuppressWarnings("rawtypes")
	public Object getInternalAdapter(Class adapter, Command command,
			IResourceChangeListener listener) {
		return null;
	}

}
